import React from 'react';
import { sysUploadUrl } from '../../../../../../Api/fileUrl'
import { postSofPdPlanActTaskChain } from "../../../../api";
import { Modal, Form, message, Input, Upload, Button, Radio } from 'antd';
import { UploadOutlined } from '@ant-design/icons'


const { TextArea } = Input;

/**
 * @description: 我的任务下发-弹窗
 * @author: xiaoxujs
 * @updata: xiaoxujs（2021-07-29）
 */

const NextModule = ({
    nextModuleShow,
    setNextModuleShow,
    handleDistributionModuleFun,
    clickId,
    clickTaskId,
    clickActId,
    clickTaskName
}) => {
    const [form] = Form.useForm()
    const { validateFields, resetFields } = form
    const handleOk = () => {
        validateFields().then(values => {
            if (values.files) {
                let arr = [];
                values.files.fileList.forEach(element => {
                    arr.push(element.response.data)
                });
                values.files = arr
            }

            values.pdplanId = clickId
            values.taskId = clickTaskId;
            values.actId = clickActId;
            ; (async () => {
                const { code, msg } = await postSofPdPlanActTaskChain(values)
                if (code === '20000') {
                    message.success('下发成功!')
                    handleDistributionModuleFun()
                    resetFields()
                    setNextModuleShow(false)
                } else {
                    message.error(msg)
                }
            })()
        })
    };

    const handleCancel = () => {
        resetFields()
        setNextModuleShow(false);
    };
    //上传
    const props = {
        name: 'file',
        action: sysUploadUrl(),
        headers: {
            authentication: sessionStorage.getItem("token")
        },
        data: { folder: '/contract' },
        onChange(info) {
            if (info.file.status === 'done') {
                message.success(`${info.file.name} 上传成功`);
            } else if (info.file.status === 'error') {
                message.error(`${info.file.name} file upload failed.`);
            }
        },
    };
    return <Modal title='下发' visible={nextModuleShow} onOk={handleOk} onCancel={handleCancel}>
        <Form
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 16 }}
            form={form}
        >
            {
                clickTaskName === '质量检测' ? <Form.Item
                    label="检测结果"
                    name="isPass"
                    rules={[{ required: true, message: '请选择是否检测通过!' }]}
                >
                    <Radio.Group>
                        <Radio value={true}>通过</Radio>
                        <Radio value={false}>不通过</Radio>
                    </Radio.Group>
                </Form.Item> : null
            }
            {
                clickTaskName === '仓库盘库' ? <Form.Item
                    label="是否采购"
                    name="isPurchase"
                    rules={[{ required: true, message: '请选择是否采购!' }]}
                >
                    <Radio.Group>
                        <Radio value={true}>是</Radio>
                        <Radio value={false}>否</Radio>
                    </Radio.Group>
                </Form.Item> : null
            }
            {
                clickTaskName === '确认执行方式'
                    ?
                    <>
                        <Form.Item
                            label="智能参与"
                            name="isSmart"
                            rules={[{ required: true, message: '请选择是否需要智能部!' }]}
                        >
                            <Radio.Group>
                                <Radio value={true}>参与</Radio>
                                <Radio value={false}>不参与</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item
                            label="是否需要外协"
                            name="isOut"
                            rules={[{ required: true, message: '请选择是否需要外协!' }]}
                        >
                            <Radio.Group>
                                <Radio value={true}>是</Radio>
                                <Radio value={false}>否</Radio>
                            </Radio.Group>
                        </Form.Item>
                    </>
                    : null
            }
            {
                clickTaskName === '外协审批' ? <Form.Item
                    label="外协是否通过"
                    name="isOutPass"
                    rules={[{ required: true, message: '请选择是否采购!' }]}
                >
                    <Radio.Group>
                        <Radio value={true}>通过</Radio>
                        <Radio value={false}>不通过</Radio>
                    </Radio.Group>
                </Form.Item> : null
            }
            <Form.Item
                label="备注"
                name="remake"
            >
                <TextArea />
            </Form.Item>
            <Form.Item
                label="上传文件"
                name="files"
            // rules={[{ required: true, message: '请上传文件!' }]}
            >
                <Upload {...props}>
                    <Button icon={<UploadOutlined />}>请上传文件</Button>
                </Upload>
            </Form.Item>
        </Form>
    </Modal>
}

export default NextModule